~ chicken-core (master) /manual/Module (chicken base)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken base)
5
6Core procedures and macros, acting as basic extensions to the R7RS
7standard and other essential features.
8
9This module is used by default, unless a program is compiled with
10the {{-explicit-use}} option.
11
12=== Numeric predicates
13
14These allow you to make a more precise differentiation between number
15types and their properties, not provided by R7RS.
16
17==== fixnum?
18
19<procedure>(fixnum? X)</procedure>
20
21Returns {{#t}} if {{X}} is a fixnum, or {{#f}} otherwise.
22
23==== flonum?
24
25<procedure>(flonum? X)</procedure>
26
27Returns {{#t}} if {{X}} is a flonum, or {{#f}} otherwise.
28
29==== bignum?
30
31<procedure>(bignum? X)</procedure>
32
33Returns {{#t}} if {{X}} is a bignum (integer larger than fits in a
34fixnum), or {{#f}} otherwise.
35
36==== exact-integer?
37
38<procedure>(exact-integer? X)</procedure>
39
40Returns {{#t}} if {{X}} is an exact integer (i.e., a fixnum or a
41bignum), or {{#f}} otherwise.
42
43This procedure is compatible with the definition from the R7RS
44{{(scheme base)}} library.
45
46==== cplxnum?
47
48<procedure>(cplxnum? X)</procedure>
49
50Returns {{#t}} if {{X}} is a true complex number (it has an imaginary
51component), or {{#f}} otherwise.
52
53Please note that {{complex?}} will always return {{#t}} for any number
54type supported by CHICKEN, so you can use this predicate if you want
55to know the representational type of a number.
56
57==== ratnum?
58
59<procedure>(ratnum? X)</procedure>
60
61Returns {{#t}} if {{X}} is a true rational number (it is a fraction
62with a denominator that's not 1), or {{#f}} otherwise.
63
64Please note that {{rational?}} will always return {{#t}} for any
65number type supported by CHICKEN except complex numbers and non-finite
66flonums, so you can use this predicate if you want to know the
67representational type of a number.
68
69==== nan?
70
71<procedure>(nan? N)</procedure>
72
73Returns {{#t}} if {{N}} is not a number (a IEEE flonum NaN-value). If
74{{N}} is a complex number, it's considered nan if it has a real or
75imaginary component that's nan.
76
77This procedure is compatible with the definition from the R7RS
78{{(scheme inexact)}} library.
79
80==== infinite?
81
82<procedure>(infinite? N)</procedure>
83
84Returns {{#t}} if {{N}} is negative or positive infinity, and {{#f}}
85otherwise. If {{N}} is a complex number, it's considered infinite if
86it has a real or imaginary component that's infinite.
87
88This procedure is compatible with the definition from the R7RS
89{{(scheme inexact)}} library.
90
91==== finite?
92
93<procedure>(finite? N)</procedure>
94
95Returns {{#t}} if {{N}} represents a finite number and {{#f}}
96otherwise. Positive and negative infinity as well as NaNs are not
97considered finite. If {{N}} is a complex number, it's considered
98finite if both the real and imaginary components are finite.
99
100This procedure is compatible with the definition from the R7RS
101{{(scheme inexact)}} library.
102
103==== equal=?
104
105<procedure>(equal=? X y)</procedure>
106
107Similar to the standard procedure {{equal?}}, but compares numbers
108using the {{=}} operator, so {{equal=?}} allows structural comparison
109in combination with comparison of numerical data by value.
110
111
112=== Arithmetic
113
114==== add1/sub1
115
116<procedure>(add1 N)</procedure>
117<procedure>(sub1 N)</procedure>
118
119Adds/subtracts 1 from {{N}}.
120
121
122==== exact-integer-nth-root
123
124<procedure>(exact-integer-nth-root K N)</procedure>
125
126Like {{exact-integer-sqrt}}, but with any base value. Calculates
127{{\sqrt[N]{K}}}, the {{N}}th root of {{K}} and returns two values
128{{s}} and {{r}} where {{s^N + r = K}} and {{K < (s+1)^N}}.
129
130
131==== Division with quotient and remainder
132
133<procedure>(quotient&remainder X Y)</procedure>
134<procedure>(quotient&modulo X Y)</procedure>
135
136Returns two values: the quotient and the remainder (or modulo) of
137{{X}} divided by {{Y}}. Could be defined as {{(values (quotient X Y)
138(remainder X Y))}}, but is much more efficient when dividing very
139large numbers.
140
141==== signum
142
143<procedure>(signum N)</procedure>
144
145For real numbers, returns {{1}} if {{N}} is positive, {{-1}} if {{N}}
146is negative or {{0}} if {{N}} is zero. {{signum}} is exactness
147preserving.
148
149For complex numbers, returns a complex number of the same angle but
150with magnitude 1.
151
152
153=== Weak pairs
154
155''Weak pairs'' behave identically to regular pairs, with one
156exception: the car value may be garbage collected. When that happens,
157it gets replaced with a sentinel "broken-weak-pointer" value.
158
159They are indistinguishable from regular pairs: {{car}}, {{cdr}}, etc
160all work on them, {{pair?}} returns true, etc. The {{WRITE}}
161representation is identical to regular pairs, so they will be read
162back as pairs. In other words, they have no read/write invariance.
163
164They're the same as regular pairs for all intents and purposes.
165However, there's a {{weak-pair?}} predicate which ''can'' distinguish
166between regular pairs and weak pairs.
167
168NOTE: Due to internal limitations, {{set-car!}} on a weak pair
169currently may cause it to hold onto the value for one more GC cycle in
170some situations.
171
172==== weak-cons
173
174<procedure>(weak-cons obj[1] obj[2])</procedure><br>
175
176Returns a newly allocated weak pair whose car is obj[1] and whose cdr
177is obj[2]. The pair is indistinguishable from normal pairs, except as
178noted above.
179
180 (weak-cons 'a '()) ===> (a)
181 (weak-cons '(a) '(b c d)) ===> ((a) b c d)
182 (weak-cons "a" '(b c)) ===> ("a" b c)
183 (weak-cons 'a 3) ===> (a . 3)
184 (weak-cons '(a b) 'c) ===> ((a b) . c)
185
186 (import (chicken gc))
187
188 (let* ((x '(a b))
189 (y (weak-cons x 'c)))
190 (gc #t)
191 (car x)) ===> (a b)
192
193 (let ((x (weak-cons '(a b) 'c)))
194 (gc #t)
195 (car x)) ===> #!bwp
196
197As the final two examples show, when something ''else'' still holds on
198to the value that's stored in the car of a weak pair, it will not be
199reclaimed. But if the value is ''only'' referenced by one or more
200weak pairs, it is reclaimed and the car of the weak pair is replaced
201with the ''broken-weak-pointer'' value {{#!bwp}}.
202
203<procedure>(weak-pair? obj)</procedure><br>
204
205This predicate returns {{#t}} if and only if {{obj}} is a weak pair.
206
207 (weak-pair? (weak-cons 'a '())) ===> #t
208 (weak-pair? (cons 'a '())) ===> #f
209 (weak-pair? (vector 'a '())) ===> #f
210
211<procedure>(bwp-object? obj)</procedure>
212
213This predicate returns {{#t}} if {{obj}} is the broken-weak-pointer
214value, otherwise {{#f}}.
215
216
217=== Input/Output
218
219==== current-error-port
220
221<procedure>(current-error-port [PORT])</procedure>
222
223Returns default error output port. If {{PORT}} is given, then that
224port is selected as the new current error output port.
225
226Note that the default error output port is not buffered. Use
227[[Module (chicken port)#set-buffering-mode!|{{set-buffering-mode!}}]]
228if you need a different behaviour.
229
230==== print
231
232<procedure>(print [EXP1 ...])</procedure>
233
234Outputs the optional arguments {{EXP1 ...}} using {{display}} and
235writes a newline character to the port that is the value of
236{{(current-output-port)}}. Returns {{(void)}}.
237
238==== print*
239
240<procedure>(print* [EXP1 ...])</procedure>
241
242Similar to {{print}}, but does not output a terminating newline
243character and performs a {{flush-output}} after writing its arguments.
244
245
246=== Interrupts and error-handling
247
248==== enable-warnings
249
250<procedure>(enable-warnings [BOOL])</procedure>
251
252Enables or disables warnings, depending on wether {{BOOL}} is true or
253false. If called with no arguments, this procedure returns {{#t}} if
254warnings are currently enabled, or {{#f}} otherwise. Note that this is
255not a parameter. The current state (whether warnings are enabled or
256disabled) is global and not thread-local.
257
258
259==== error
260
261<procedure>(error [LOCATION] [STRING] EXP ...)</procedure>
262
263Prints error message, writes all extra arguments to the value of
264{{(current-error-port)}} and invokes the current
265exception-handler. This conforms to
266[[http://srfi.schemers.org/srfi-23/srfi-23.html|SRFI-23]]. If
267{{LOCATION}} is given and a symbol, it specifies the ''location'' (the
268name of the procedure) where the error occurred.
269
270
271==== assert
272
273<macro>(assert EXP [OBJ ...])</macro>
274
275Evaluates {{EXP}}, if it returns #f, {{error}} is applied to {{OBJ ...}},
276else the result of {{EXP}} is returned.
277When compiling in unsafe mode, assertions of this kind are disabled.
278
279
280==== get-call-chain
281
282<procedure>(get-call-chain [START [THREAD]])</procedure>
283
284Returns a list with the call history. Backtrace information is only
285generated in code compiled without {{-no-trace}} and evaluated code.
286If the optional argument {{START}} is given, the backtrace starts at
287this offset, i.e. when {{START}} is 1, the next to last trace-entry is
288printed, and so on. If the optional argument {{THREAD}} is given, then
289the call-chain will only be constructed for calls performed by this
290thread.
291
292
293
294==== print-call-chain
295
296<procedure>(print-call-chain [PORT [START [THREAD [HEADER]]]])</procedure>
297
298Prints a backtrace of the procedure call history to {{PORT}}, which
299defaults to {{(current-output-port)}}. The output is prefixed by the
300{{HEADER}}, which defaults to {{"\n\tCall history:\n"}}.
301
302
303==== procedure-information
304
305<procedure>(procedure-information PROC)</procedure>
306
307Returns an s-expression with debug information for the procedure
308{{PROC}}, or {{#f}}, if {{PROC}} has no associated debug information.
309
310
311==== warning
312
313<procedure>(warning MESSAGE [EXP ...])</procedure>
314
315Displays a warning message (if warnings are enabled with {{enable-warnings}}),
316from the {{MESSAGE}}, and optional {{EXP}} arguments, then continues execution.
317{{MESSAGE}}, and {{EXP}}, may be {{any}} object.
318
319
320=== Lists
321
322==== alist-ref
323
324<procedure>(alist-ref KEY ALIST [TEST [DEFAULT]])</procedure>
325
326Looks up {{KEY}} in {{ALIST}} using {{TEST}} as the comparison function (or {{eqv?}} if
327no test was given) and returns the cdr of the found pair, or {{DEFAULT}} (which defaults to {{#f}}).
328
329
330==== alist-update
331
332<procedure>(alist-update KEY VALUE ALIST [TEST])</procedure>
333<procedure>(alist-update! KEY VALUE ALIST [TEST])</procedure>
334
335If the list {{ALIST}} contains a pair of the form {{(KEY . X)}}, then this procedure
336replaces {{X}} with {{VALUE}} and returns {{ALIST}}. If {{ALIST}} contains no such item, then
337{{alist-update}} returns {{((KEY . VALUE) . ALIST)}}. The optional argument
338{{TEST}} specifies the comparison procedure to search a matching pair in {{ALIST}}
339and defaults to {{eqv?}}. {{alist-update!}} is the destructive version of {{alist-update}}.
340
341
342==== atom?
343
344<procedure>(atom? X)</procedure>
345
346Returns {{#t}} if {{X}} is not a pair.
347
348
349==== butlast
350
351<procedure>(butlast LIST)</procedure>
352
353Returns a fresh list with all elements but the last of {{LIST}}.
354
355
356==== chop
357
358<procedure>(chop LIST N)</procedure>
359
360Returns a new list of sublists, where each sublist contains {{N}}
361elements of {{LIST}}. If {{LIST}} has a length that is not
362a multiple of {{N}}, then the last sublist contains the remaining
363elements.
364
365<enscript highlight=scheme>
366(chop '(1 2 3 4 5 6) 2) ==> ((1 2) (3 4) (5 6))
367(chop '(a b c d) 3) ==> ((a b c) (d))
368</enscript>
369
370
371==== compress
372
373<procedure>(compress BLIST LIST)</procedure>
374
375Returns a new list with elements taken from {{LIST}} with
376corresponding true values in the list {{BLIST}}.
377
378<enscript highlight=scheme>
379(define nums '(99 100 110 401 1234))
380(compress (map odd? nums) nums) ==> (99 401)
381</enscript>
382
383
384==== flatten
385
386<procedure>(flatten LIST1 ...)</procedure>
387
388Returns {{LIST1 ...}} concatenated together, with nested lists
389removed (flattened).
390
391
392==== foldl
393
394<procedure>(foldl PROCEDURE INIT LIST)</procedure>
395
396Applies {{PROCEDURE}} to the elements from {{LIST}}, beginning from
397the left:
398
399<enscript hightlight=scheme>
400(foldl + 0 '(1 2 3)) ==> (+ (+ (+ 0 1) 2) 3)
401</enscript>
402
403Note that the order of arguments taken by {{PROCEDURE}} is different
404from the {{SRFI-1}} {{fold}} procedure, but matches the more natural
405order used in Haskell and Objective Caml.
406
407
408==== foldr
409
410<procedure>(foldr PROCEDURE INIT LIST)</procedure>
411
412Applies {{PROCEDURE}} to the elements from {{LIST}}, beginning from
413the right:
414
415<enscript hightlight=scheme>
416(foldr + 0 '(1 2 3)) ==> (+ 1 (+ 2 (+ 3 0)))
417</enscript>
418
419
420==== intersperse
421
422<procedure>(intersperse LIST X)</procedure>
423
424Returns a new list with {{X}} placed between each element.
425
426
427==== join
428
429<procedure>(join LISTOFLISTS [LIST])</procedure>
430
431Concatenates the lists in {{LISTOFLISTS}} with {{LIST}} placed
432between each sublist. {{LIST}} defaults to the empty list.
433
434<enscript highlight=scheme>
435(join '((a b) (c d) (e)) '(x y)) ==> (a b x y c d x y e)
436(join '((p q) () (r (s) t)) '(-)) ==> (p q - - r (s) t)
437</enscript>
438
439{{join}} could be implemented as follows:
440
441<enscript highlight=scheme>
442(define (join lstoflsts #!optional (lst '()))
443 (apply append (intersperse lstoflists lst)) )
444</enscript>
445
446
447==== rassoc
448
449<procedure>(rassoc KEY LIST [TEST])</procedure>
450
451Similar to {{assoc}}, but compares {{KEY}} with the {{cdr}} of each pair in {{LIST}} using
452{{TEST}} as the comparison procedures (which defaults to {{eqv?}}).
453
454
455==== tail?
456
457<procedure>(tail? X LIST)</procedure>
458
459Returns true if {{X}} is one of the tails (cdr's) of {{LIST}}.
460
461
462=== Vectors
463
464==== vector-resize
465
466<procedure>(vector-resize VECTOR N [INIT])</procedure>
467
468Creates and returns a new vector with the contents of {{VECTOR}} and
469length {{N}}. If {{N}} is greater than the original length of
470{{VECTOR}}, then all additional items are initialized to {{INIT}}. If
471{{INIT}} is not specified, the contents are initialized to some
472unspecified value.
473
474
475==== subvector
476
477<procedure>(subvector VECTOR FROM [TO])</procedure>
478
479Returns a new vector with elements taken from {{VECTOR}} in the
480given range. {{TO}} defaults to {{(vector-length VECTOR)}}.
481
482{{subvector}} was introduced in CHICKEN 4.7.3.
483
484
485=== Combinators
486
487
488==== constantly
489
490<procedure>(constantly X ...)</procedure>
491
492Returns a procedure that always returns the values {{X ...}} regardless of the number and value of its arguments.
493
494<enscript highlight=scheme>
495(constantly X) <=> (lambda args X)
496</enscript>
497
498
499==== complement
500
501<procedure>(complement PROC)</procedure>
502
503Returns a procedure that returns the boolean inverse of {{PROC}}.
504
505<enscript highlight=scheme>
506(complement PROC) <=> (lambda (x) (not (PROC x)))
507</enscript>
508
509
510==== compose
511
512<procedure>(compose PROC1 PROC2 ...)</procedure>
513
514Returns a procedure that represents the composition of the
515argument-procedures {{PROC1 PROC2 ...}}.
516
517<enscript highlight=scheme>
518(compose F G) <=> (lambda args
519 (call-with-values
520 (lambda () (apply G args))
521 F))
522</enscript>
523
524{{(compose)}} is equivalent to {{values}}.
525
526
527==== conjoin
528
529<procedure>(conjoin PRED ...)</procedure>
530
531Returns a procedure that returns {{#t}} if its argument satisfies the
532predicates {{PRED ...}}.
533<enscript highlight=scheme>
534((conjoin odd? positive?) 33) ==> #t
535((conjoin odd? positive?) -33) ==> #f
536</enscript>
537
538
539==== disjoin
540
541<procedure>(disjoin PRED ...)</procedure>
542
543Returns a procedure that returns {{#t}} if its argument satisfies any
544predicate {{PRED ...}}.
545<enscript highlight=scheme>
546((disjoin odd? positive?) 32) ==> #t
547((disjoin odd? positive?) -32) ==> #f
548</enscript>
549
550
551==== each
552
553<procedure>(each PROC ...)</procedure>
554
555Returns a procedure that applies {{PROC ...}} to its arguments, and returns the result(s)
556of the last procedure application. For example
557
558<enscript highlight=scheme>
559(each pp eval)
560</enscript>
561
562is equivalent to
563
564<enscript highlight=scheme>
565(lambda args
566 (apply pp args)
567 (apply eval args) )
568</enscript>
569
570{{(each PROC)}} is equivalent to {{PROC}} and {{(each)}} is equivalent to
571{{void}}.
572
573
574==== flip
575
576<procedure>(flip PROC)</procedure>
577
578Returns a two-argument procedure that calls {{PROC}} with its
579arguments swapped:
580<enscript highlight=scheme>
581(flip PROC) <=> (lambda (x y) (PROC y x))
582</enscript>
583
584
585==== identity
586
587<procedure>(identity X)</procedure>
588
589Returns its sole argument {{X}}.
590
591
592==== list-of?
593
594<procedure>(list-of? PRED)</procedure>
595
596Returns a procedure of one argument that returns {{#t}} when
597applied to a list of elements that all satisfy the predicate procedure
598{{PRED}}, or {{#f}} otherwise.
599
600<enscript highlight=scheme>
601((list-of? even?) '(1 2 3)) ==> #f
602((list-of? number?) '(1 2 3)) ==> #t
603</enscript>
604
605
606==== o
607
608<procedure>(o PROC ...)</procedure>
609
610A single value version of {{compose}} (slightly faster). {{(o)}} is equivalent
611to {{identity}}.
612
613
614=== UNICODE case folding
615
616==== char-foldcase
617
618<procedure>(char-foldcase CHAR)</procedure>
619
620Performs simple UNICODE case folding to {{CHAR}}, language-specific mappings
621are not used.
622
623==== string-foldcase
624
625<procedure>(string-foldcase STRING)</procedure>
626
627Performs UNICODE case folding to {{STRING}}, language-specific mappings
628are not used. The resulting string may be longer than the original string.
629
630
631=== User-defined named characters
632
633==== char-name
634
635<procedure>(char-name SYMBOL-OR-CHAR [CHAR])</procedure>
636
637This procedure can be used to inquire about character names or to
638define new ones. With a single argument the behavior is as follows:
639If {{SYMBOL-OR-CHAR}} is a symbol, then {{char-name}} returns
640the character with this name, or {{#f}} if no character is defined
641under this name. If {{SYMBOL-OR-CHAR}} is a character, then the
642name of the character is returned as a symbol, or {{#f}} if the
643character has no associated name.
644
645If the optional argument {{CHAR}} is provided, then
646{{SYMBOL-OR-CHAR}} should be a symbol that will be the new name of
647the given character. If multiple names designate the same character,
648then the {{write}} will use the character name that was defined last.
649
650<enscript highlight=scheme>
651(char-name 'space) ==> #\space
652(char-name #\space) ==> space
653(char-name 'bell) ==> #f
654(char-name (integer->char 7)) ==> #f
655(char-name 'bell (integer->char 7))
656(char-name 'bell) ==> #\bell
657(char->integer (char-name 'bell)) ==> 7
658</enscript>
659
660
661=== The unspecified value
662
663==== void
664
665<procedure>(void ARGUMENT ...)</procedure>
666
667Ignores {{ARGUMENT ...}} and returns an unspecified value.
668
669
670=== Continuations
671
672==== call/cc
673
674<procedure>(call/cc PROCEDURE)</procedure>
675
676An alias for {{call-with-current-continuation}}.
677
678This procedure is compatible with the definition from the R7RS
679{{(scheme base)}} library.
680
681=== Symbols
682
683==== Symbol utilities
684
685===== symbol-append
686
687<procedure>(symbol-append SYMBOL1 ...)</procedure>
688
689Creates a new symbol from the concatenated names of the argument symbols
690{{(SYMBOL1 ...)}}.
691
692==== Uninterned symbols ("gensyms")
693
694Symbols may be "interned" or "uninterned". Interned symbols are
695registered in a global table, and when read back from a port are
696identical to a symbol written before:
697
698<enscript highlight=scheme>
699(define sym 'foo)
700
701(eq? sym (with-input-from-string
702 (with-output-to-string
703 (lambda () (write sym)))
704 read))
705
706 => #t
707</enscript>
708
709Uninterned symbols on the other hand are not globally registered and so
710multiple symbols with the same name may coexist:
711
712<enscript highlight=scheme>
713(define sym (gensym 'foo)) ; sym is a uninterned symbol like "foo42"
714
715(eq? sym (with-input-from-string ; the symbol read will be an interned symbol
716 (with-output-to-string
717 (lambda () (write sym)))
718 read))
719
720 => #f
721
722(eq? (string->uninterned-symbol "foo") (string->uninterned-symbol "foo"))
723
724 => #f
725</enscript>
726
727Use uninterned symbols if you need to generate unique values that
728can be compared quickly, for example as keys into a hash-table
729or association list. Note that uninterned symbols lose their
730uniqueness property when written to a file and read back in, as
731in the example above.
732
733===== gensym
734
735<procedure>(gensym [STRING-OR-SYMBOL])</procedure>
736
737Returns a newly created uninterned symbol. If an argument is provided,
738the new symbol is prefixed with that argument.
739
740
741===== string->uninterned-symbol
742
743<procedure>(string->uninterned-symbol STRING)</procedure>
744
745Returns a newly created, unique symbol with the name {{STRING}}.
746
747
748=== Setters
749
750SRFI-17 is fully implemented. For more information see:
751[[http://srfi.schemers.org/srfi-17/srfi-17.html|SRFI-17]].
752
753==== setter
754
755<procedure>(setter PROCEDURE)</procedure>
756
757Returns the setter-procedure of {{PROCEDURE}}, or signals an error if
758{{PROCEDURE}} has no associated setter-procedure.
759
760Note that {{(set! (setter PROC) ...)}} for a procedure that has no
761associated setter procedure yet is a very slow operation (the old
762procedure is replaced by a modified copy, which involves a garbage
763collection).
764
765
766==== getter-with-setter
767
768<procedure>(getter-with-setter GETTER SETTER)</procedure>
769
770Returns a copy of the procedure {{GETTER}} with the associated setter
771procedure {{SETTER}}. Contrary to the SRFI specification, the setter
772of the returned procedure may be changed.
773
774
775=== Binding forms for optional arguments
776
777==== optional
778
779<macro>(optional ARGS DEFAULT)</macro>
780
781Use this form for procedures that take a single optional argument. If
782{{ARGS}} is the empty list {{DEFAULT}} is evaluated and
783returned, otherwise the first element of the list {{ARGS}}. It is
784an error if {{ARGS}} contains more than one value.
785
786<enscript highlight=scheme>
787(define (incr x . i) (+ x (optional i 1)))
788(incr 10) ==> 11
789(incr 12 5) ==> 17
790</enscript>
791
792
793==== let-optionals
794
795<macro> (let-optionals ARGS ((VAR1 DEFAULT1) ...) BODY ...)</macro>
796
797Binding constructs for optional procedure arguments. {{ARGS}} is
798normally a rest-parameter taken from a lambda-list. {{let-optionals}}
799binds {{VAR1 ...}} to available arguments in parallel, or to
800{{DEFAULT1 ...}} if not enough arguments were provided.
801{{let-optionals*}} binds {{VAR1 ...}} sequentially, so every variable
802sees the previous ones. it is an error if any excess arguments are
803provided.
804
805<enscript highlight=scheme>
806(let-optionals '(one two) ((a 1) (b 2) (c 3))
807 (list a b c) ) ==> (one two 3)
808</enscript>
809
810==== let-optionals*
811
812<macro> (let-optionals* ARGS ((VAR1 DEFAULT1) ... [RESTVAR]) BODY ...)</macro>
813
814Binding constructs for optional procedure arguments. {{ARGS}} is
815normally a rest-parameter taken from a lambda-list. {{let-optionals}}
816binds {{VAR1 ...}} to available arguments in parallel, or to
817{{DEFAULT1 ...}} if not enough arguments were provided.
818{{let-optionals*}} binds {{VAR1 ...}} sequentially, so every variable
819sees the previous ones. If a single variable {{RESTVAR}} is given,
820then it is bound to any remaining arguments, otherwise it is an error
821if any excess arguments are provided.
822
823<enscript highlight=scheme>
824(let-optionals* '(one two) ((a 1) (b 2) (c a))
825 (list a b c) ) ==> (one two one)
826</enscript>
827
828=== Other binding forms
829
830==== and-let*
831
832<macro>(and-let* (BINDING ...) EXP1 EXP2 ...)</macro>
833
834Bind sequentially and execute body. {{BINDING}} can
835be a list of a variable and an expression, a list with a single
836expression, or a single variable. If the value of an expression
837bound to a variable is {{#f}}, the {{and-let*}} form
838evaluates to {{#f}} (and the subsequent bindings and the body
839are not executed). Otherwise the next binding is performed. If
840all bindings/expressions evaluate to a true result, the body is
841executed normally and the result of the last expression is the
842result of the {{and-let*}} form. See also the documentation for
843[[http://srfi.schemers.org/srfi-2/srfi-2.html|SRFI-2]].
844
845==== letrec*
846
847<macro>(letrec* ((VARIABLE EXPRESSION) ...) BODY ...)</macro>
848
849Implements R6RS/R7RS {{letrec*}}. {{letrec*}} is similar to {{letrec}}
850but binds the variables sequentially and is to {{letrec}} what
851{{let*}} is to {{let}}.
852
853This special form is compatible with the definition from the R7RS
854{{(scheme base)}} library.
855
856==== rec
857
858<macro>(rec NAME EXPRESSION)</macro><br>
859<macro>(rec (NAME VARIABLE ...) BODY ...)</macro>
860
861Allows simple definition of recursive definitions. {{(rec NAME EXPRESSION)}} is
862equivalent to {{(letrec ((NAME EXPRESSION)) NAME)}} and {{(rec (NAME VARIABLE ...) BODY ...)}}
863is the same as {{(letrec ((NAME (lambda (VARIABLE ...) BODY ...))) NAME)}}.
864
865==== cut
866
867<macro>(cut SLOT ...)</macro><br>
868<macro>(cute SLOT ...)</macro>
869
870[[http://srfi.schemers.org/srfi-26/srfi-26.html|Syntactic sugar for specializing parameters]].
871
872==== define-values
873
874<macro>(define-values (NAME ...) VALUEEXP)</macro>
875<macro>(define-values (NAME1 ... NAMEn . NAMEn+1) VALUEEXP)</macro>
876<macro>(define-values NAME VALUEEXP)</macro>
877
878Defines several variables at once, with the result values of expression
879{{VALUEEXP}}, similar to {{set!-values}}.
880
881This special form is compatible with the definition from the R7RS
882{{(scheme base)}} library.
883
884==== fluid-let
885
886<macro>(fluid-let ((VAR1 X1) ...) BODY ...)</macro>
887
888Binds the variables {{VAR1 ...}} dynamically to the values {{X1 ...}}
889during execution of {{BODY ...}}. This implements
890[[http://srfi.schemers.org/srfi-15/srfi-15.html|SRFI-15]].
891
892==== let-values
893
894<macro>(let-values (((NAME ...) VALUEEXP) ...) BODY ...)</macro>
895
896Binds multiple variables to the result values of {{VALUEEXP ...}}.
897All variables are bound simultaneously. Like {{define-values}}, the
898{{(NAME ...)}} expression can be any basic lambda list (dotted tail
899notation is supported).
900
901This special form implements
902[[http://srfi.schemers.org/srfi-11/srfi-11.html|SRFI-11]], and it is
903also compatible with the definition from the R7RS {{(scheme base)}}
904library.
905
906
907==== let*-values
908
909<macro>(let*-values (((NAME ...) VALUEEXP) ...) BODY ...)</macro>
910
911Binds multiple variables to the result values of {{VALUEEXP ...}}.
912The variables are bound sequentially. Like {{let-values}}, the
913{{(NAME ...)}} expression can be any basic lambda list (dotted tail
914notation is supported).
915
916This is also part of
917[[http://srfi.schemers.org/srfi-11/srfi-11.html|SRFI-11]] and is also
918compatible with the definition from the R7RS {{(scheme base)}}
919library.
920
921<enscript highlight=scheme>
922(let*-values (((a b) (values 2 3))
923 ((p) (+ a b)) )
924 p) ==> 5
925</enscript>
926
927==== letrec-values
928
929<macro>(letrec-values (((NAME ...) VALUEEXP) ...) BODY ...)</macro>
930
931Binds the result values of {{VALUEEXP ...}} to multiple variables at
932once. All variables are mutually recursive. Like {{let-values}}, the
933{{(NAME ...)}} expression can be any basic lambda list (dotted tail
934notation is supported).
935
936<enscript highlight=scheme>
937(letrec-values (((odd even)
938 (values
939 (lambda (n) (if (zero? n) #f (even (sub1 n))))
940 (lambda (n) (if (zero? n) #t (odd (sub1 n)))) ) ) )
941 (odd 17) ) ==> #t
942</enscript>
943
944
945==== receive
946
947<macro>(receive (NAME ...) VALUEEXP BODY ...)</macro><br>
948<macro>(receive (NAME1 ... NAMEn . NAMEn+1) VALUEEXP BODY ...)</macro><br>
949<macro>(receive NAME VALUEEXP BODY ...)</macro><br>
950<macro>(receive VALUEEXP)</macro>
951
952[[http://srfi.schemers.org/srfi-8/srfi-8.html|SRFI-8]].
953Syntactic sugar for {{call-with-values}}. Binds variables
954to the result values of {{VALUEEXP}} and evaluates {{BODY ...}},
955similar {{define-values}} but lexically scoped.
956
957{{(receive VALUEEXP)}} is equivalent to {{(receive _ VALUEEXP _)}}.
958This shortened form is not described by SRFI-8.
959
960==== set!-values
961
962<macro>(set!-values (NAME ...) VALUEEXP)</macro>
963<macro>(set!-values (NAME1 ... NAMEn . NAMEn+1) VALUEEXP)</macro>
964<macro>(set!-values NAME VALUEEXP)</macro>
965
966Assigns the result values of expression {{VALUEEXP}} to multiple
967variables, similar to {{define-values}}.
968
969==== nth-value
970
971<macro>(nth-value N EXP)</macro>
972
973Returns the {{N}}th value (counting from zero) of the values returned
974by expression {{EXP}}.
975
976
977=== Substitution forms and macros
978
979==== define-constant
980
981<macro>(define-constant NAME CONST)</macro>
982
983Defines a variable with a constant value, evaluated at compile-time.
984Any reference to such a constant should appear textually '''after'''
985its definition. This construct is equivalent to {{define}} when
986evaluated or interpreted. Constant definitions should only appear at
987toplevel. Note that constants are local to the current compilation
988unit and are not available outside of the source file in which they
989are defined. Names of constants still exist in the Scheme namespace
990and can be lexically shadowed. If the value is mutable, then the
991compiler is careful to preserve its identity. {{CONST}} may be any
992constant expression, and may also refer to constants defined via
993{{define-constant}} previously, but it must be possible to
994evaluate the expression at compile-time.
995
996==== define-inline
997
998<macro>(define-inline (NAME VAR ...) BODY ...)</macro><br>
999<macro>(define-inline (NAME VAR ... . VAR) BODY ...)</macro><br>
1000<macro>(define-inline NAME EXP)</macro>
1001
1002Defines an inline procedure. Any occurrence of {{NAME}} will be replaced
1003by {{EXP}} or {{(lambda (VAR ... [. VAR]) BODY ...)}}. This is similar
1004to a macro, but variable names and scope are handled correctly.
1005
1006Inline substitutions take place '''after''' macro-expansion, and any
1007reference to {{NAME}} should appear textually '''after''' its
1008definition. Inline procedures are local to the current compilation unit
1009and are not available outside of the source file in which they are
1010defined. Names of inline procedures still exist in the Scheme namespace
1011and can be lexically shadowed. Inline definitions should only appear at
1012the toplevel.
1013
1014Note that the {{inline-limit}} compiler option does not affect inline
1015procedure expansion, and self-referential inline procedures may cause
1016the compiler to enter an infinite loop.
1017
1018In the third form, {{EXP}} must be a lambda expression.
1019
1020This construct is equivalent to {{define}} when evaluated or
1021interpreted.
1022
1023
1024=== Conditional forms
1025
1026==== unless
1027
1028<macro>(unless TEST EXP1 EXP2 ...)</macro>
1029
1030Equivalent to:
1031
1032<enscript highlight=scheme>
1033(if (not TEST) (begin EXP1 EXP2 ...))
1034</enscript>
1035
1036==== when
1037
1038<macro>(when TEST EXP1 EXP2 ...)</macro>
1039
1040Equivalent to:
1041
1042<enscript highlight=scheme>
1043(if TEST (begin EXP1 EXP2 ...))
1044</enscript>
1045
1046=== Record structures
1047
1048==== define-record
1049
1050<macro>(define-record NAME SLOTNAME ...)</macro>
1051
1052Defines a record type. This defines a number of procedures for
1053creating, accessing, and modifying record members.
1054
1055Call {{make-NAME}} to create an instance
1056of the structure (with one initialization-argument for each slot, in
1057the listed order).
1058
1059{{(NAME? STRUCT)}} tests any object for being an instance of this
1060structure.
1061
1062Slots are accessed via {{(NAME-SLOTNAME STRUCT)}}
1063and updated using {{(NAME-SLOTNAME-set!}} {{STRUCT}} {{VALUE)}}.
1064
1065<enscript highlight=scheme>
1066(define-record point x y)
1067(define p1 (make-point 123 456))
1068(point? p1) ==> #t
1069(point-x p1) ==> 123
1070(point-y-set! p1 99)
1071(point-y p1) ==> 99
1072</enscript>
1073
1074===== SRFI-17 setters
1075
1076{{SLOTNAME}} may alternatively also be of the form
1077
1078 (setter SLOTNAME)
1079
1080In this case the slot can be read with {{(NAME-SLOTNAME STRUCT)}} as usual,
1081and modified with {{(set! (NAME-SLOTNAME STRUCT) VALUE)}} (the slot-accessor
1082has an associated SRFI-17 "setter" procedure) instead of
1083the usual {{(NAME-SLOTNAME-set!}} {{STRUCT}} {{VALUE)}}.
1084
1085
1086<enscript highlight=scheme>
1087(define-record point (setter x) (setter y))
1088(define p1 (make-point 123 456))
1089(point? p1) ==> #t
1090(point-x p1) ==> 123
1091(set! (point-y p1) 99)
1092(point-y p1) ==> 99
1093</enscript>
1094
1095==== define-record-type
1096
1097<macro>(define-record-type NAME (CONSTRUCTOR TAG ...) PREDICATE (FIELD ACCESSOR [MODIFIER]) ...)</macro>
1098
1099SRFI-9 record types. For more information see the documentation for
1100[[http://srfi.schemers.org/srfi-9/srfi-9.html|SRFI-9]].
1101
1102As an extension the {{MODIFIER}} may have the form
1103{{(setter PROCEDURE)}}, which will define a SRFI-17 setter-procedure
1104for the given {{PROCEDURE}} that sets the field value.
1105Usually {{PROCEDURE}} has the same name is {{ACCESSOR}} (but it
1106doesn't have to).
1107
1108This special form is also compatible with the definition from the R7RS
1109{{(scheme base)}} library.
1110
1111==== record-printer
1112
1113<procedure>(record-printer NAME)</procedure><br>
1114
1115Returns the procedure used to print records of the type {{NAME}} if
1116one has been set with {{set-record-printer!}}, {{#f}} otherwise.
1117
1118==== set-record-printer!
1119
1120<procedure>(set-record-printer! NAME PROCEDURE)</procedure><br>
1121<procedure>(set! (record-printer NAME) PROCEDURE)</procedure>
1122
1123Defines a printing method for record of the type {{NAME}} by
1124associating a procedure with the record type. When a record of this
1125type is written using {{display, write}} or {{print}}, then
1126the procedure is called with two arguments: the record to be printed
1127and an output-port.
1128
1129<enscript highlight=scheme>
1130(define-record-type foo (make-foo x y z) foo?
1131 (x foo-x)
1132 (y foo-y)
1133 (z foo-z))
1134(define f (make-foo 1 2 3))
1135(set-record-printer! foo
1136 (lambda (x out)
1137 (fprintf out "#,(foo ~S ~S ~S)"
1138 (foo-x x) (foo-y x) (foo-z x))))
1139(define-reader-ctor 'foo make-foo)
1140(define s (with-output-to-string
1141 (lambda () (write f))))
1142s ==> "#,(foo 1 2 3)"
1143(equal? f (with-input-from-string
1144 s read))) ==> #t
1145</enscript>
1146
1147=== Other forms
1148
1149==== load
1150
1151<procedure>(load filename [evalproc])</procedure><br>
1152
1153Filename should be a string naming an existing file containing Scheme
1154source code. The load procedure reads expressions and definitions from
1155the file and evaluates them sequentially. It is unspecified whether the
1156results of the expressions are printed. The load procedure does not
1157affect the values returned by current-input-port and
1158current-output-port. Load returns an unspecified value.
1159
1160CHICKEN offers a few extensions to the R7RS definition of {{load}}:
1161
1162* The {{filename}} may also be an input port.
1163* The expressions which are read one by one from the source file are passed to the procedure indicated by the extra optional {{evalproc}} argument, which defaults to {{eval}}.
1164* On platforms that support it (currently BSD, Haiku, MacOS X, Linux, Solaris, and Windows), {{load}} can be used to load shared objects.
1165
1166Example for loading compiled programs:
1167
1168 % cat x.scm
1169 (define (hello) (print "Hello!"))
1170 % csc -s x.scm
1171 % csi -q
1172 #;1> (load "x.so")
1173 ; loading x.so ...
1174 #;2> (hello)
1175 Hello!
1176 #;3>
1177
1178There are some limitations and caveats to the CHICKEN extensions you
1179need to be aware of:
1180
1181* The second argument to {{load}} is ignored when loading compiled code.
1182* If source code is loaded from a port, then that port is closed after all expressions have been read.
1183* A compiled file can only be loaded once. Subsequent attempts to load the same file have no effect.
1184
1185==== include
1186
1187<macro>(include STRING1 STRING2 ...)</macro>
1188<macro>(include-ci STRING1 STRING2 ...)</macro>
1189
1190Include toplevel-expressions from the given source files in the currently
1191compiled/interpreted program. If the included file has the extension
1192{{.scm}}, then it may be omitted. The file is searched for in the
1193current directory and all directories specified by the {{-include-path}}
1194option.
1195
1196{{include-ci}} works as {{include}} but reads files in case-insensitive
1197mode.
1198
1199==== include-relative
1200
1201<macro>(include-relative STRING)</macro>
1202
1203Works like {{include}}, but the filename is searched for relative to the
1204including file rather than the current directory.
1205
1206
1207=== Making extra libraries and extensions available
1208
1209==== require-extension
1210
1211<macro>(require-extension ID ...)</macro>
1212
1213This is equivalent to {{(require-library ID ...)}} but performs an implicit
1214{{import}}, if necessary. Since version 4.4.0, {{ID}} may also be an import specification
1215(using {{rename}}, {{only}}, {{except}} or {{prefix}}).
1216
1217To make long matters short - just use {{require-extension}} and it will normally figure everything out for dynamically
1218loadable extensions and core library units.
1219
1220This implementation of {{require-extension}} is compliant with [[http://srfi.schemers.org/srfi-55/srfi-55.html|SRFI-55]]
1221(see the [[http://srfi.schemers.org/srfi-55/srfi-55.html|SRFI-55]] document for more information).
1222
1223
1224==== require-library
1225
1226<macro>(require-library ID ...)</macro>
1227
1228This form does all the necessary steps to make the libraries or extensions given
1229in {{ID ...}} available. It loads syntactic extensions, if needed and generates
1230code for loading/linking with core library modules or separately installed
1231extensions.
1232
1233During interpretation/evaluation {{require-library}} performs one of the
1234following:
1235
1236* If {{ID}} names a built-in feature, then nothing is done.
1237* If {{ID}} names one of the syntactic extensions {{chicken-syntax chicken-ffi-syntax}}, then this extension will be loaded.
1238* If {{ID}} names one of the core library units shipped with CHICKEN, then a {{(load-library 'ID)}} will be performed.
1239* If {{ID}} names an installed extension with the {{syntax}} or {{require-at-runtime}} attribute, then the extensions is loaded at compile-time, probably doing a run-time {{(require ...)}} for any run-time requirements.
1240* Otherwise, {{(require-library ID)}} is equivalent to {{(require 'ID)}}.
1241
1242During compilation, one of the following happens instead:
1243
1244* If {{ID}} names a built-in feature, then nothing is done.
1245* If {{ID}} names one of the syntactic extensions {{chicken-syntax chicken-ffi-syntax}}, then this extension will be loaded at compile-time, making the syntactic extensions available in compiled code.
1246* If {{ID}} names one of the core library units shipped with CHICKEN, or if the option {{-uses ID}} has been passed to the compiler, then a {{(declare (uses ID))}} is generated.
1247* If {{ID}} names an installed extension with the {{syntax}} or {{require-at-runtime}} attribute, then the extension is loaded at compile-time, and code is emitted to {{(require ...)}} any needed run-time requirements.
1248* Otherwise {{(require-library ID)}} is equivalent to {{(require 'ID)}}.
1249
1250{{ID}} should be a pure extension name and should not contain any path prefixes (for example {{dir/lib...}} is illegal).
1251
1252{{ID}} may also be a list that designates an extension-specifier. Currently the following extension specifiers are
1253defined:
1254
1255* {{(srfi NUMBER ...)}} is required for SRFI-55 compatibility and is fully implemented
1256* {{(version ID NUMBER)}} is equivalent to {{ID}}, but checks at compile-time whether the extension named {{ID}} is installed and whether its version is equal or higher than {{NUMBER}}. {{NUMBER}} may be a string or a number, the comparison is done lexicographically (using {{string>=?}}).
1257
1258=== Process shutdown
1259
1260==== emergency-exit
1261
1262<procedure>(emergency-exit [CODE])</procedure>
1263
1264Exits the current process without flushing any buffered output (using
1265the C function {{_exit}}). Note that the {{exit-handler}} is not called
1266when this procedure is invoked. The optional exit status code {{CODE}}
1267defaults to {{0}}.
1268
1269
1270==== exit
1271
1272<procedure>(exit [CODE])</procedure>
1273
1274Exit the running process and return exit-code, which defaults to 0
1275(Invokes {{exit-handler}}).
1276
1277Note that pending {{dynamic-wind}} thunks are ''not'' invoked when exiting your program in this way.
1278
1279
1280=== exit-handler
1281
1282<parameter>(exit-handler)</parameter>
1283
1284A procedure of a single optional argument. When {{exit}} is called,
1285then this procedure will be invoked with the exit-code as argument. The
1286default behavior is to terminate the program.
1287
1288Note that this handler is ''not'' invoked when {{emergency-exit}} is
1289used.
1290
1291
1292=== implicit-exit-handler
1293
1294<parameter>(implicit-exit-handler)</parameter>
1295
1296A procedure of no arguments. When the last toplevel expression of the
1297program has executed, then the value of this parameter is called. The
1298default behaviour is to invoke all pending finalizers.
1299
1300
1301==== on-exit
1302
1303<procedure>(on-exit THUNK)</procedure>
1304
1305Schedules the zero-argument procedures {{THUNK}} to be executed before
1306the process exits, either explicitly via {{exit}} or implicitly after
1307execution of the last top-level form. Note that finalizers for
1308unreferenced finalized data are run before exit procedures.
1309
1310
1311=== System interface
1312
1313
1314==== sleep
1315
1316<procedure>(sleep SECONDS)</procedure>
1317
1318Puts the program to sleep for {{SECONDS}}. If the scheduler is loaded
1319(for example when srfi-18 is in use) then only the calling thread is put
1320to sleep and other threads may continue executing. Otherwise, the whole
1321process is put to sleep.
1322
1323
1324=== File Input/Output
1325
1326==== flush-output
1327
1328<procedure>(flush-output [PORT])</procedure>
1329
1330Write buffered output to the given output-port. {{PORT}} defaults
1331to the value of {{(current-output-port)}}.
1332
1333=== Port predicates
1334
1335==== port-closed?
1336
1337<procedure>(port-closed? PORT)</procedure>
1338
1339Is the given {{PORT}} closed (in all directions)?
1340
1341
1342=== Built-in parameters
1343
1344Certain behavior of the interpreter and compiled programs can be
1345customized via the following built-in parameters:
1346
1347==== case-sensitive
1348
1349<parameter>(case-sensitive)</parameter>
1350
1351If true, then {{read}} reads symbols and identifiers in
1352case-sensitive mode and uppercase characters in symbols are printed
1353escaped. Defaults to {{#t}}.
1354
1355
1356==== keyword-style
1357
1358<parameter>(keyword-style)</parameter>
1359
1360Enables alternative keyword syntax, where {{STYLE}} may be either
1361{{#:prefix}} (as in Common Lisp), which recognizes symbols beginning
1362with a colon as keywords, or {{#:suffix}} (as in DSSSL), which recognizes
1363symbols ending with a colon as keywords.
1364Any other value disables the alternative syntaxes. In the interpreter
1365the default is {{#:suffix}}.
1366
1367
1368==== parentheses-synonyms
1369
1370<parameter>(parentheses-synonyms)</parameter>
1371
1372If true, then the list delimiter synonyms {{#\[}} {{#\]}} and {{#\{}} {{#\}}} are enabled. Defaults to {{#t}}.
1373
1374
1375==== symbol-escape
1376
1377<parameter>(symbol-escape)</parameter>
1378
1379If true, then the symbol escape {{#\|}} {{#\|}} is allowed when reading
1380and printing expressions. Defaults to {{#t}}.
1381
1382
1383---
1384Previous: [[Module srfi-4]]
1385
1386Next: [[Module (chicken bitwise)]]